package br.usp.ime.origami.algorithms;

import br.usp.ime.origami.primitives.NumberPrimitives;

public class Baskara {

	private double a, b, c;

	private double delta;

	private double firstRoot;

	private double secondRoot;

	public Baskara(double a, double b, double c) {
		this.a = a;
		this.b = b;
		this.c = c;
		this.delta = b * b - 4 * a * c;
		if (a == 0) {
			if (b == 0) {
				// sistema indeterminado
				this.delta = -1;
			} else {
				this.secondRoot = this.firstRoot = -c / b;
			}
		} else {
			if (NumberPrimitives.isPositive(delta)) {
				if (delta < 0)
					delta = 0;
				this.firstRoot = (-b - Math.sqrt(delta)) / (2 * a);
				this.secondRoot = (-b + Math.sqrt(delta)) / (2 * a);
			} else {
				// TODO creio q nao precisa fazer nada ja q o delta ta negativo
			}
		}
	}

	public boolean hasRealRoots() {
		return this.delta >= 0;
	}

	public double getFirstRoot() {
		return firstRoot;
	}

	public double getSecondRoot() {
		return secondRoot;
	}

	public double getDelta() {
		return delta;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return String.format("[Equation a=%f b=%f c=%f delta=%f roots[%f %f]]", a, b, c, delta, firstRoot, secondRoot);
	}
}
